package com.example.fragment.tool;

import android.app.PendingIntent;
import android.content.Context;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.util.Log;

import java.util.HashMap;
import java.util.Iterator;

public class USBcom {

    private static final String TAG ="" ;
    private UsbManager usbManager;
    private UsbDevice usbDevice;
    private  UsbEndpoint epBulkOut;
    private  UsbEndpoint epBulkIn;
    private  UsbEndpoint epControl;
    private UsbEndpoint epIntEndpointOut;
    private UsbEndpoint epIntEndpointIn;
    private PendingIntent intent;

    /**
     * 找到自定设备
     */
    public void findUSB(Context mContext,int VENDORID, int PRODUCTID,byte[] buffer) {
        //1)创建usbManager
        usbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
        //2)获取到所有设备 选择出满足的设备
        HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        while (deviceIterator.hasNext()) {
            UsbDevice device = deviceIterator.next();
            Log.i(TAG, "vendorID--" + device.getVendorId() + "ProductId--" + device.getProductId());
            if (device.getVendorId() == VENDORID && device.getProductId() == PRODUCTID) {
                usbDevice = device; // 获取USBDevice
            }
            UsbInterface usbInterface = null;
            for (int i = 0; i < usbDevice.getInterfaceCount(); i++) {
                //一个设备上面一般只有一个接口，有两个端点，分别接受和发送数据
                usbInterface = usbDevice.getInterface(i);
                break;
            }


            //获取usb设备通信信道
            for (int i = 0; i < usbInterface.getEndpointCount(); i++) {
                UsbEndpoint ep = usbInterface.getEndpoint(i);
                switch (ep.getType()) {
                    case UsbConstants.USB_ENDPOINT_XFER_BULK://USB端口传输
                        if (UsbConstants.USB_DIR_OUT == ep.getDirection()) {//输出
                             epBulkOut = ep;
                            Log.e(TAG, "获取发送数据的端点");
                        } else {
                            epBulkIn = ep;
                            Log.e(TAG, "获取接受数据的端点");
                        }
                        break;
                    case UsbConstants.USB_ENDPOINT_XFER_CONTROL://控制
                        epControl = ep;
                        Log.e(TAG, "find the ControlEndPoint:" + "index:" + i + "," + epControl.getEndpointNumber());
                        break;
                    case UsbConstants.USB_ENDPOINT_XFER_INT://中断
                        if (ep.getDirection() == UsbConstants.USB_DIR_OUT) {//输出
                             epIntEndpointOut = ep;
                            Log.e(TAG, "find the InterruptEndpointOut:" + "index:" + i + "," + epIntEndpointOut.getEndpointNumber());
                        }
                        if (ep.getDirection() == UsbConstants.USB_DIR_IN) {
                            epIntEndpointIn = ep;
                            Log.e(TAG, "find the InterruptEndpointIn:" + "index:" + i + "," + epIntEndpointIn.getEndpointNumber());
                        }
                        break;
                    default:
                        break;
                }
            }
            UsbDeviceConnection conn = null;
            if (usbManager.hasPermission(usbDevice)) {
                //有权限，那么打开
                conn = usbManager.openDevice(usbDevice);
            } else {
                usbManager.requestPermission(usbDevice, intent);
                if (usbManager.hasPermission(usbDevice)) { //权限获取成功
                    conn = usbManager.openDevice(usbDevice);
                } else {
                    Log.e(TAG, "没有权限");
                }
                if (conn.claimInterface(usbInterface, true)) {
                    if (conn != null)// 到此你的android设备已经连上zigbee设备
                        Log.i(TAG, "open设备成功！");
                    final String mySerial = conn.getSerial();
                    Log.i(TAG, "设备serial number：" + mySerial);
                } else {
                    Log.i(TAG, "无法打开连接通道。");
                    conn.close();
                }
                if (conn == null || epBulkOut == null) return;
                if (conn.bulkTransfer(epBulkOut, buffer, buffer.length, 0) >= 0) {
                    //0 或者正数表示成功
                    Log.i(TAG, "发送成功");
                } else {
                    Log.i(TAG, "发送失败的");
                }
                conn.close();

            }

        }
    }


}
